package com.tvunetworks.center.user.controller;

import com.tvunetworks.center.common.model.LoginUser;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.user.annotation.Logined;
import com.tvunetworks.center.user.model.param.AddRoleUserParam;
import com.tvunetworks.center.user.model.param.GetSonUserNoRelationRoleIdParam;
import com.tvunetworks.center.user.model.param.GetUserByRoleIdParam;
import com.tvunetworks.center.user.service.RoleUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author Etan Yang
 * @data: 2021/4/12 14:17
 * @program: tvucc-aggregator
 * @description:
 */

@RestController
@RequestMapping("/roleUser")
public class RoleUserController {
    @Autowired
    private RoleUserService roleUserService;

    /**
     * 查询指定role下的所有子账号
     * @param loginUser loginUser
     * @param param param
     * @return Result
     */
    @PostMapping("pageUserByRoleId")
    public Result<Object> pageUserByRoleId(@Logined LoginUser loginUser, @Valid@RequestBody GetUserByRoleIdParam param) {
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        return roleUserService.pageUserByRoleId(loginUser, param);
    }

    /**
     * 给指定role添加子账号
     * @param loginUser loginUser
     * @param param param
     * @return Result
     */
    @PostMapping("add")
    public Result<Object> add(@Logined LoginUser loginUser, @Valid @RequestBody AddRoleUserParam param) {
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        return roleUserService.add(loginUser, param);
    }

    /**
     * 删除role下的子账号
     * @param loginUser loginUser
     * @param userId userId
     * @return Result
     */
    @PostMapping("delete")
    public Result<Object> delete(@Logined LoginUser loginUser, @RequestParam("userId") String userId) {
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        return roleUserService.delete(loginUser, userId);
    }

    /**
     *
     * @param loginUser loginUser
     * @param param param
     * @return Result
     */
    @PostMapping("getSonUserNoRelationRoleId")
    public Result<Object> getSonUserNoRelationRoleId(@Logined LoginUser loginUser, @Valid @RequestBody GetSonUserNoRelationRoleIdParam param) {
        if (loginUser == null) {
            return ResultUtil.noLogin();
        }
        return roleUserService.getSonUserNoRelationRoleId(loginUser, param);
    }

}
